package basic.chapter6;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Kevin
 * @date 2020/2/21 23:21
 */
public class BlockedQueue<T> {

    final Lock lock = new ReentrantLock();
    //条件变量：队列不满
    final Condition notFull = lock.newCondition();
    //条件变量：队列不空
    final Condition notEmpty = lock.newCondition();
    //标识队列是否已满
    boolean full  = true;
    //标识队列是够已空
    boolean empty = true;

    //入队
    void enq(T x) {
        lock.lock();
        try {
            while(full) {
                //等待队列不满
                notFull.await();
            }
            //省略入队操作...
            //入队后，通知可出队
            notEmpty.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    // 出队
    void deq(){
        lock.lock();
        try {
            while (empty){
                // 等待队列不空
                notEmpty.await();
            }
            // 省略出队操作...
            //出队后，通知可入队
            notFull.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

